package com.ptkc.core.esearch.repos;

import com.ptkc.core.esearch.model.Activities;
import com.ptkc.core.esearch.model.ActivityTarget;
import com.ptkc.utils.ESUtil;
import org.elasticsearch.action.DocWriteResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.script.Script;
import org.elasticsearch.script.ScriptType;
import org.springframework.cglib.beans.BeanMap;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author: Mr.Jiao
 * @date: 2018年12月06日
 */
@Component
public class ActivitiesRepository {

    private RestHighLevelClient restHighLevelClient;

    private final String INDEX = "runfast_activity";

    private final String TYPE = "activity";

    private ESUtil esUtil;

    public ActivitiesRepository(RestHighLevelClient restHighLevelClient){
        this.restHighLevelClient = restHighLevelClient;
        this.esUtil = new ESUtil(restHighLevelClient);
    }

    public boolean save(Activities activities) throws IOException {
        if(activities != null) {
            DocWriteResponse response = this.esUtil.save(INDEX, TYPE, String.valueOf(activities.getId()), activities);
            if (response.status() == RestStatus.OK) {
                return true;
            }
        }
        return false;
    }

    public boolean delete(Activities activities) throws IOException {
        DeleteRequest deleteRequest = new DeleteRequest(INDEX, TYPE, String.valueOf(activities.getId()));
        DeleteResponse response = restHighLevelClient.delete(deleteRequest);
        if(response.status() == RestStatus.OK){
            return true;
        }
        return false;
    }


//    public List<Activities> getBusinessActivities(Integer businessId) throws IOException {
//        BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
//        queryBuilder.must(QueryBuilders.termQuery("target.businessId", businessId));
//        queryBuilder.mustNot(QueryBuilders.termQuery("target.deleted", true));
//        NestedQueryBuilder nestedQueryBuilder = QueryBuilders.nestedQuery("target", queryBuilder, ScoreMode.None);
//        BoolQueryBuilder parentQueryBuilder = QueryBuilders.boolQuery();
//        parentQueryBuilder.must(QueryBuilders.rangeQuery("endTime").gt(System.currentTimeMillis()));
//        parentQueryBuilder.must(nestedQueryBuilder);
//        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
//        sourceBuilder.query(parentQueryBuilder);
//        SearchRequest searchRequest = new SearchRequest();
//        searchRequest.indices(INDEX).types(TYPE).source(sourceBuilder);
//        SearchResponse response = this.restHighLevelClient.search(searchRequest);
//        List<Activities> activities = new ArrayList<>();
//        if(response.status() == RestStatus.OK){
//            SearchHits searchHit = response.getHits();
//            if(searchHit != null){
//                SearchHit[] hits = searchHit.getHits();
//                if(hits != null){
//                    for(SearchHit hit : hits){
//                        String strValue = hit.getSourceAsString();
//                        try{
//                            Activities acti = JSON.parseObject(strValue, Activities.class);
//                            activities.add(acti);
//                        }catch (Throwable e){
//                            e.printStackTrace();
//                        }
//                    }
//                }
//            }
//        }
//        return activities;
//    }
//
//
//    public Activities getActivity(String id){
//        GetRequest request = new GetRequest(INDEX, TYPE, id);
//        try {
//            GetResponse response = restHighLevelClient.get(request);
//            String strValue = response.getSourceAsString();
//            return JSON.parseObject(strValue, Activities.class);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        return null;
//    }


    public boolean saveActivityTarget(ActivityTarget target) throws IOException {
        UpdateRequest request = new UpdateRequest(INDEX, TYPE, String.valueOf(target.getActivityId()));
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("target", BeanMap.create(target));
        paramMap.put("id", target.getId());
        Script script = new Script(ScriptType.STORED, null, "runfast_activity_target_save", paramMap);
        request.script(script);
        UpdateResponse response = this.restHighLevelClient.update(request);
        if(response.status() == RestStatus.OK){
            return true;
        }
        return false;
    }


    public boolean delActivityTarget(ActivityTarget target) throws IOException {
        UpdateRequest request = new UpdateRequest(INDEX, TYPE, String.valueOf(target.getActivityId()));
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("activityId", target.getActivityId());
        paramMap.put("id", target.getId());
        Script script = new Script(ScriptType.STORED, null, "runfast_activity_target_del", paramMap);
        request.script(script);
        UpdateResponse response = this.restHighLevelClient.update(request);
        if(response.status() == RestStatus.OK){
            return true;
        }
        return false;
    }

}
